package com.right.health.app.ui.household;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.TimeUtil;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.bean.BloodPressureDetailResponse;
import com.right.health.app.bean.BloodPressureResponse;
import com.right.health.app.common.DataCommon;
import com.right.health.app.constants.BroadCastCode;
import com.right.health.app.constants.BroadCastKey;
import com.right.health.app.net.HouseholdApi;

/*
 *  血压详情
 *
 * Created by hopeliao on 2019-10-02
 *
 */
public class BloodPressureDetailActivity extends BaseActivity implements OnHttpListener {

    private static final int HTTP_CODE_DELETE = 1;
    private static final int HTTP_CODE_DETAIL = 2;

    private HouseholdApi mHouseholdApi;

    private BloodPressureResponse.BloodPressureInfo mResponse;

    private TextView mDetailTimeTv;

    private TextView mValueTv1, mValueTv2, mValueTv3;
    private TextView mStateTv;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.blood_pressure_detail);
    }

    @Override
    public void findView() {
        mDetailTimeTv = (TextView) findViewById(R.id.tv_time);
        mValueTv1 = (TextView) findViewById(R.id.tv_value1); //收缩压
        mValueTv2 = (TextView) findViewById(R.id.tv_value2); //舒张压
        mValueTv3 = (TextView) findViewById(R.id.tv_value3); // 脉搏
        mStateTv = (TextView) findViewById(R.id.tv_state); // 血压状态

        findViewById(R.id.tv_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataCommon.getDeleteDialog(BloodPressureDetailActivity.this, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        if (mResponse != null) {
                            mHouseholdApi.addRequestCode(HTTP_CODE_DELETE);
                            mHouseholdApi.deleteBloodPressure(mResponse.medicalrecordid);
                        }
                    }
                }).show();
            }
        });
    }

    @Override
    public void initialize() {
        String id = getIntent().getExtras().getString(BroadCastKey.DATA);

        mHouseholdApi = new HouseholdApi();
        mHouseholdApi.setListener(this);

        mHouseholdApi.addRequestCode(HTTP_CODE_DETAIL);
        mHouseholdApi.getBloodPressureDetail(id);
    }

    private void refreshUI() {
        if (mResponse != null) {
            findViewById(R.id.ll_content).setVisibility(View.VISIBLE);
            findViewById(R.id.tv_delete).setVisibility(View.VISIBLE);
            mDetailTimeTv.setText(TimeUtil.convertTime(TimeUtil.TIME_FORMAT, mResponse.createtime));

            mValueTv1.setText(mResponse.highpressure);
            mValueTv2.setText(mResponse.lowvoltage);

            mResponse.refreshState(mStateTv);
            mValueTv3.setText(mResponse.pulse);
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            if (request.requestCode == HTTP_CODE_DELETE) {
                ToastUtil.show(this, "删除成功");
                sendBroadcast(BroadCastCode.REFRESH_COMMON);
                finish();
            } else if (request.requestCode == HTTP_CODE_DETAIL) {
                if (response.targetData instanceof BloodPressureDetailResponse) {
                    BloodPressureDetailResponse detailResponse = response.cast(BloodPressureDetailResponse.class);
                    mResponse = detailResponse.bloodPressure;
                    refreshUI();
                }
            }
        }
    }
}
